package com.space.school.controller;


import com.space.school.domain.User;
import com.space.school.service.UserService;
import com.space.school.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserOperateServlet",value = "/UserOperateServlet")
public class UserOperateServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String userId = req.getParameter("userId");
        String operate = req.getParameter("operate");
//        删除操作
        if(operate.equals("del")){
            userService.del(Integer.parseInt(userId));
//            相当于调了一个doGet方法
//            Redirect 重新定向
            resp.sendRedirect("/userServlet");
        }else if(operate.equals("update")){
            User user=userService.find(Integer.parseInt(userId));
            if(user!=null){
                req.setAttribute("user",user);
                req.getRequestDispatcher("updateUserList.jsp").forward(req,resp);
            }
        }else if(operate.equals("status")){
            String status = req.getParameter("status");
            User user=new User();
            user.setStatus(status);
            user.setUserId(Integer.parseInt(userId));
            userService.update_status(user);
            req.setAttribute("user",user);
            resp.sendRedirect("/userServlet");
        }

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        String passWord = req.getParameter("userNewPassword");
        User user=new User();
        user.setUserId(Integer.parseInt(userId));
        user.setPassWord(passWord);
        userService.update(user);
        resp.sendRedirect("/productInfoByPageServlet");
    }
}
